package com.hiema.controll;

import com.heima.common.dtos.ResponseResult;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.user.entity.FollowConstant;
import com.hiema.entity.BehaviorDto;
import com.hiema.entity.RedisObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class VehaviorControll {
    @Autowired
    private StringRedisTemplate redisTemplate;

    public static final String OPERATION_LIKE="OPERATIONLIKE";
    public static final String TYPE="TYPE2023";

    @PostMapping("/api/v1/likes_behavior")
    public ResponseResult like(@RequestBody BehaviorDto dto){
      //获取文章id
        Long articleId = dto.getArticleId();

        String userIdandarticleId=""+articleId;
         //获取类型
        Integer type = dto.getType();

        //点赞
        Integer operation = dto.getOperation();

         //使用hash类型redis
         HashOperations opsForHash = redisTemplate.opsForHash();
         //添加数据
        //判断用户的状态
        if (operation==0){
            //如果为0，表示用户进行关注操作,将其存储到缓存中
            redisTemplate.opsForHash().put(userIdandarticleId,OPERATION_LIKE, FollowConstant.FOLLOW_OPERATION_CANCEL);
        }else {
            //如果不为0，表示用户进行取消关注操作,方法将其存储到缓存中
            redisTemplate.opsForHash().put(userIdandarticleId,OPERATION_LIKE, FollowConstant.FOLLOW_OPERATION_SUCCESS);

        }


        //判断用户的状态
        if (type==0){
            //如果为0，表示用户进行关注操作,将其存储到缓存中
            redisTemplate.opsForHash().put(userIdandarticleId,TYPE, FollowConstant.FOLLOW_OPERATION_SUCCESS);
        }else {
            //如果不为0，表示用户进行取消关注操作,方法将其存储到缓存中
            redisTemplate.opsForHash().put(userIdandarticleId,TYPE, FollowConstant.FOLLOW_OPERATION_CANCEL);

        }


         return  ResponseResult.ok();

     }



    public static final String READ="READ";



    /**
     * 阅读文章次数记录
     * */
    @PostMapping("/api/v1/read_behavior")
    public ResponseResult readArticle(@RequestBody RedisObject redisObject){

        //文章id
        long articleld = redisObject.getArticleId();
        //大key
        String name=articleld+"";

        //先查询出一个redis
        //根据大小key 获取的阅读次数 可能是0也可能不是0 不重要 是不是0都得+1  不做判断


        if (ObjectUtils.isEmpty( redisTemplate.opsForHash().get(name, READ))){

            redisTemplate.opsForHash().put(name,READ,"1");

        }else {
            String num = (String)redisTemplate.opsForHash().get(name, READ);
            int s= Integer.parseInt(num)+1;
            String ss=s+"";
            redisTemplate.opsForHash().put(name,READ,ss);

        }


        //阅读页数添加到哈希redis对象

        return ResponseResult.ok();
    }





    }

